package view.role
{
	import control.manager.LayerManager;
	import control.manager.MapManager;
	import control.manager.RoleManager;
	import control.util.CollisionBitmap;
	import control.util.GameUtil;
	
	import data.MapData;
	import data.PlayerData;
	import data.RoleVO;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	import view.item.AnimationObj;
	import view.item.RoleLife;
	import view.layer.GameLayer;
	
	public class MapObject extends Sprite
	{
		
		public var curStatus :String = null;
		
		/**
		 * 角色数据 
		 */		
		public var roleData :*;
		/**
		 * 角色uid,唯一值 
		 */		
		public var uid :String;
		/**
		 * 是否存活 
		 */		
		public var isAlive :Boolean = true;
		/**
		 * 角色名字文本 
		 */		
		public var txtRoleName :TextField;
		/**
		 * 动画播放类 
		 */		
		public var animationObj :AnimationObj;
		/**
		 * 角色当前动作 动画播放
		 */		
		public var curAction :String = RoleVO.WAIT;
		/**
		 * 角色当前方向 
		 */		
		public var curDirection :int = RoleVO.DOWN;
		/**
		 * 角色生命值显示条 
		 */		
		public var roleLife :RoleLife;
		/**
		 * 用于判断鼠标是否在对象像，支持透明和模糊判断  
		 */		
		public var mouseCollision :CollisionBitmap;
		/**
		 * 当前点击的对象 
		 */		
		public var curClickObj :MapObject;
		/**
		 * 鼠标移上去显示 
		 */		
		public static const MOUSE_ON:Array = [new GlowFilter(0xffff00,1,8,8,2,2)];
		/**
		 * 鼠标移上 
		 */		
		private var _mouseOn :Boolean;
		public function MapObject(roleData :*)
		{
			super();
//			this.mouseChildren = this.mouseEnabled = false;
			this.mouseCollision = new CollisionBitmap(this);
			this.roleData = roleData;
			//动画播放器
			this.animationObj = new AnimationObj(roleData,curAction,curDirection,this);
			this.addChild(this.animationObj);
			//角色名字文本
			this.txtRoleName = new TextField();
			this.txtRoleName.selectable = false;
			this.txtRoleName.mouseEnabled = false;
			this.txtRoleName.defaultTextFormat = new TextFormat(null,14,0,true,null,null,null,null,TextFormatAlign.CENTER);
			this.txtRoleName.width = 80;
			this.txtRoleName.height = 20;
			this.txtRoleName.multiline = false;
			this.txtRoleName.x = -this.animationObj.width/2 - Math.abs(this.txtRoleName.width - this.animationObj.width)/2;
			this.txtRoleName.y = -this.animationObj.height - 5;
			this.txtRoleName.text = roleData.roleName;
			this.addChild(this.txtRoleName);
			
			var value :Boolean
			switch(roleData.roleType)
			{
				case RoleVO.PLAYER:
					initRoleLife();
					break;
				case RoleVO.NPC:
					value = GameUtil.instance.isNull();
					if(!value)
					{
						this.uid = roleData.roleType + GameUtil.instance.getRandom();
					}else
					{
						trace("设置uid失败，没有随机数了");
					}
					break;
				case RoleVO.MONSTER:
					initRoleLife();
					value  = GameUtil.instance.isNull();
					if(!value)
					{
						this.uid = roleData.roleType + GameUtil.instance.getRandom();
					}else
					{
						trace("设置uid失败，没有随机数了");
					}
					break;
			}
			
			
			addEventListener(MouseEvent.MOUSE_OVER,mouseHandler);
			addEventListener(MouseEvent.MOUSE_OUT,mouseHandler);
			addEventListener(MouseEvent.MOUSE_DOWN,mouseHandler);
			addEventListener(MouseEvent.MOUSE_UP,mouseHandler);
		}
		/**
		 * 设置生命条 
		 * @param value
		 * 
		 */		
		public function setRoleLife(value :Number,hp :int):void
		{
			if(this.roleLife)
			{
				
			}
		}
		
		public function hurt(value :Number):void
		{
		}
		/**
		 * 更新函数，播放动画== 
		 * 
		 */		
		public function update():void
		{
			if(this.animationObj)
			{
				this.animationObj.update();
			}
		}
		/**
		 * 点击
		 */		
		public function onClick(e :* = null):void
		{
			var clickObj :*
			if(e is MouseEvent)
			{
				clickObj = e.currentTarget;
				trace(e.currentTarget);
			}else
			{
				clickObj = e;
				trace(e);
			}
			var mapObj :MapObject;
			if(clickObj is MapPlayer) return;
			
			if(clickObj is MapNPC)
			{
				mapObj = clickObj as MapNPC;
			}else if(clickObj is MapMonster)
			{
				mapObj = clickObj as MapMonster;
			}
			var gameLayer :GameLayer = LayerManager.instance.getLayer(GameLayer.NAME) as GameLayer;
			var player :MapPlayer = gameLayer.player;
			var startPoint :Point = new Point(player.x,player.y);
			var targetPoint :Point = mapObj.position;
			var distance :Number = Point.distance(startPoint,targetPoint);
			
			if(distance > player.playerData.playerATTRange)   //需要移动
			{
				player.findPath(startPoint,targetPoint);
				
				player.curClickObj = mapObj;
				player.curStatus = MapPlayer.MOVE;
			}else  //不需要移动
			{
				player.curClickObj = mapObj;
				if(mapObj is MapNPC)
				{
					player.curStatus = MapPlayer.SHOW_TALK;
					player.showTalk();
				}else if(mapObj is MapMonster)
				{
					player.attMonster();
				}
			}
				
		}
		/**
		 * 获取正常位置 
		 * @return 
		 * 
		 */		
		public function get position():Point
		{
			return new Point(this.x,this.y);
		}

		/**
		 * 获取被攻击时的偏移点 
		 * @return 
		 * 
		 */		
		public function get attPosition():Point
		{
			return new Point(this.x - 10,this.y - 60);
		}
		
		public function destroy():void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,mouseHandler);
			removeEventListener(MouseEvent.MOUSE_OUT,mouseHandler);
			removeEventListener(MouseEvent.MOUSE_DOWN,mouseHandler);
			removeEventListener(MouseEvent.MOUSE_UP,mouseHandler);
			this.animationObj = null;
			this.mouseCollision = null;
		}
		

		public function get mouseOn():Boolean
		{
			return _mouseOn;
		}

		public function set mouseOn(value:Boolean):void
		{
			_mouseOn = value;
			if(value)
			{
				this.animationObj.filters = MOUSE_ON;
			}else
			{
				this.animationObj.filters = null;
			}
			
		}
		
		private function initRoleLife():void
		{
			
			this.roleLife = new RoleLife(this.animationObj);
			this.addChild(this.roleLife);
		}
		
		private function mouseHandler(e :MouseEvent):void
		{
			switch(e.type)
			{
				case MouseEvent.MOUSE_OVER:
					mouseOn = true;
					break;
				case MouseEvent.MOUSE_OUT:
					mouseOn = false;
					break;
				case MouseEvent.MOUSE_DOWN:
					break;
				case MouseEvent.MOUSE_UP:
					onClick(e);
					break;
			}
		}
		

	}
}